/**
 * service_user js file;
 * company:junqitecch
 * author:xuzuiotao
 * 
 */

//test
//
// LOGOUT:32,
//        USER_DETAIL:33,
//        USER_CHANGE_PASSWORD:34,


/**
 * login js file;
 * company:junqitecch
 * author:xuzuiotao
 * handling response information
 */

var  consts = require('../consts');
var mail = require('../network/email');
var users= require('../db/users');
var cache=require('../redis/usercache');
var tournote= require('../mongo/tournote');
var qiniu=require('../network/qiniu');
var im =require('../network/im');
var tournote_user=require('../mongo/tournote_user');
var contacts = require('../db/contact');
var member = require('../db/member');

function processBuildScanURL(request,response,callback){
	var data =request.data;
	var muuid=null;
	var token=null;
	if(request.muuid===null || typeof(request.muuid)==='undefined'){
		
	}else{
		muuid=request.muuid;
	}
	
	if(request.token===null || typeof(request.token)==='undefined'){
		
	}else{
		token =request.token;
	}
	
	if(muuid===null && token ===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':muuid or token';
		if(callback){
			callback(response);
		}
		return;	
	}
	var param = null;
	
	try {
	    param = JSON.parse(data);
	} catch (e) {
	}	
	
	var deskNO=null;
	var roomNO=null;
	var BID=null;

    if(param !==null && typeof(param.deskNO)!=='undefined' && param.deskNO!==null ){
		deskNO=param.deskNO;
	}
    
    if(param !==null && typeof(param.roomNO)!=='undefined' && param.roomNO!==null ){
		roomNO=param.roomNO;
	}
	
   if(param !==null && typeof(param.BID)!=='undefined' && param.BID!==null ){
		BID=param.BID;
	}
    
	users.buildScanURL(muuid,token, deskNO,roomNO,BID, function(obj){
		if(obj===null){
			response.errorCode=consts.Error.ERROR_PARAM;
			response.message=consts.Message.ERROR_PARAM+'';
			if(callback){
				callback(response);
			}
		}else{
			 response.errorCode = consts.Error.NONE_ERROR;
			 response.message= consts.Message.OK_MESSAGE;
			 response.data = JSON.stringify(obj);
			 if(callback){
				callback(response);
			} 
		}
	});
		
}

function processUsersLook(request,response,callback){
	console.log('users');
	
	var data =request.data;
	if(data===null || typeof(data)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;
	}else {
		console.log(data);
	}
    var param = null;
    
    try {
    	param = JSON.parse(data);
	} catch (e) {
	}

	if(param===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;
	}

	if(param.userIDs ===null || typeof(param.userIDs)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':userIDs';
		if(callback){
			callback(response);
		}
		return false;
	}
		
	users.getEntityByIDs(param.userIDs,function(wrap){
			if(entity===null){
				response.errorCode=consts.Error.ERROR_GENERAL;
				response.message=consts.Message.ERROR_USER_UNFOUND;

				if(callback){
					callback(response);
				}
				
			}else{
				
				response.errorCode = consts.Error.NONE_ERROR;
				response.message= consts.Message.OK_MESSAGE;
				response.data=JSON.stringify(wrap);
				if(callback){
				   callback(response);
		    	}
				
//				tournote.listTourNoteByUserID(param.userID,0,-1,10,function(err,content){
//					response.errorCode = consts.Error.NONE_ERROR;
//					response.message= consts.Message.OK_MESSAGE;
//			        if(content ==null ||typeof(content)=='undefined'){
//			        	content=new Object();
//			        }
//					if(content){
//						content.user=simple;
//						response.data = JSON.stringify(content);
//					}
//					if(callback){
//						callback(response);
//					}
//					
//				});
					
			}
	
	});

}



function processUserLook(request,response,callback){
	console.log('processUserDetail');
	
	var data =request.data;
	if(data===null || typeof(data)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;

	}else {
		console.log(data);
	}
    var param = null;
    
    try {
    	param = JSON.parse(data);
	} catch (e) {
	}

	if(param===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;
	}

	if(param.UID ===null || typeof(param.UID)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':UID';
		if(callback){
			callback(response);
		}
		return false;
	}
		
	var simple = {};
//	simple.ID=param.ID;//entity.userID;
	simple.UID=param.UID;
	simple.nickName='新起点';
	simple.iconURL=consts.Url.MOBILE+ '/img/icon_cata0.png';
	simple.gender=1;
	simple.birthDate=Date.now()-20*365*1000*24*3600;
	simple.mobile='13693149429';
	simple.level=1;
	simple.gender=1;
	simple.totalNum=3;
	simple.totalSum=50.00;
	
	simple.realName='闫亮';
	simple.latestBillTime=Date.now()-3600*1000*10;
	
	var content = {};
	content.user=simple;
	response.data = JSON.stringify(content);
	response.errorCode = consts.Error.NONE_ERROR;
	response.message= consts.Message.OK_MESSAGE;
	if(callback){
		callback(response);
	}
	
	return;
		
//	users.getEntityByID(param.userID,function(entity){
//			if(entity==null){
//				response.errorCode=consts.Error.ERROR_GENERAL;
//				response.message=consts.Message.ERROR_USER_UNFOUND;
//				
//				if(callback){
//					callback(response);
//				}
//				
//			}else{
//				
//				simple = new Object();
//				simple.ID=entity.ID;
//				simple.nickName=entity.nickName;
//				simple.iconURL=entity.iconURL;
//				simple.birthDate=Date.now()-20*365*1000*24*3600;
//				simple.mobile='13693149429';
//				simple.level=1;
//				simple.gender=1;
//				simple.totalNum=3;
//				simple.totalSum=50.00;
//				simple.realName='闫亮';
//				simple.latestBillTime=Date.now()-3600*1000*10;
//
//				tournote.listTourNoteByUserID(param.userID,0,-1,10,function(err,content){
//					response.errorCode = consts.Error.NONE_ERROR;
//					response.message= consts.Message.OK_MESSAGE;
//			        if(content ==null ||typeof(content)=='undefined'){
//			        	content=new Object();
//			        }
//					if(content){
//						content.user=simple;
//						response.data = JSON.stringify(content);
//					}
//					if(callback){
//						callback(response);
//					}
//					
//				});
//					
//			}
//	
//	});
}




function processRefundUpdatePwd(request,response,callback){
	console.log('processRefundUpdatePwd');
	
	var data =request.data;
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	var param = null;
	try {
		param = JSON.parse(data);
	}catch (e) {
		param = null;
	}
	if(param === null || param.oldRefundPwd===null || typeof(param.oldRefundPwd)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message='需要oldRefundPwd';//consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	
	if(param === null ||  param.newRefundPwd===null || typeof(param.newRefundPwd)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message='需要newRefundPwd';//consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	
	users.updateRefundPwd(request.token,param.oldRefundPwd, param.newRefundPwd, function(err){
		if(err){

			response.errorCode=-1;
			response.message='更改数据错误1，请重新登录试试';
			if(callback){
				callback(response);
			}
		}else{
			
			response.errorCode = consts.Error.NONE_ERROR;
			response.message= consts.Message.OK_MESSAGE;
		
			response.token=request.token;
			if(callback){
				callback(response);
			}
			return;
			
		}
		
	});

	return;
	
}


function processRefundVerifyPwd(request,response,callback){
	
	var data =request.data;
	console.log('processRefundVerifyPwd+::'+ request.token+'::'+ data);

	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	
	var param = null;
	try {
		param = JSON.parse(data);
	}catch (e) {
		param = null;
	}
	
	if(param === null || param.refundPwd===null || typeof(param.refundPwd)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message='需要refundPwd';//consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	users.verifyRefundPwd(request.token, param.refundPwd, function(err){
		if(err){

			response.errorCode=-1;
			response.message='密码验证错误';
			if(callback){
				callback(response);
			}
		}else{
			
			response.errorCode = consts.Error.NONE_ERROR;
			response.message= consts.Message.OK_MESSAGE;

			response.token=request.token;
			if(callback){
				callback(response);
			}
			return;
			
		}
		
	});

	return;
	
}


function processMyShopLook(request,response,callback){
	console.log('processMyShop');
	
	var data =request.data;
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	
	users.getMyShop(request.token, function(shop){
		if(shop===null){

			response.errorCode=-1;
			response.message='更改数据错误2，请重新登录试试';
			if(callback){
				callback(response);
			}
		}else{
			
			response.errorCode = consts.Error.NONE_ERROR;
			response.message= consts.Message.OK_MESSAGE;
		
			response.data = JSON.stringify(shop);

			response.token=request.token;
			if(callback){
				callback(response);
			}
			return;
			
		}
		
	});

	return;
	
}



function processCompleteService(request,response,callback){
	
	console.log('processCompleteService');
	var data =request.data;
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}

	var param = null;
	try {
	    param = JSON.parse(data);
	} catch (e) {
		
	}
	
	if(param===null){
			response.errorCode=consts.Error.ERROR_PARAM;
			response.message=consts.Message.ERROR_PARAM;
			if(callback){
				callback(response);
			}
			return false;
	}

	if((param.IDs ===null || typeof(param.IDs)==='undefined') && (param.orderNOs ===null || typeof(param.orderNOs)==='undefined')) {
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':IDs or orderNOs';
		if(callback){
				callback(response);
		}
		return false;	
	}
	
	users.completeService(request.token, param, function(shop){
		if(shop===null){

			response.errorCode=-1;
			response.message='更改数据错误3，请重新登录试试';
			if(callback){
				callback(response);
			}
		}else{						
			response.errorCode = consts.Error.NONE_ERROR;
			response.message= consts.Message.OK_MESSAGE;
		
//			response.data = JSON.stringify(shop);

			if(callback){
				callback(response);

			}

			return;
			
		}
		
	});

	return;
	
}

function processCompleteService1(request,response,callback){
	
	console.log('processCompleteService');
	var data =request.data;
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}

	var param = null;
	try {
	    param = JSON.parse(data);
	} catch (e) {
		
	}
	
	if(param===null){
			response.errorCode=consts.Error.ERROR_PARAM;
			response.message=consts.Message.ERROR_PARAM;
			if(callback){
				callback(response);
			}
			return false;
	}

	if((param.IDs ===null || typeof(param.IDs)==='undefined') && (param.orderNOs ===null || typeof(param.orderNOs)==='undefined')) {
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':IDs or orderNOs';
		if(callback){
				callback(response);
		}
		return false;	
	}
	
	users.completeService1(request.token, param, function(shop){
		if(shop===null){

			response.errorCode=-1;
			response.message='更改数据错误3，请重新登录试试';
			if(callback){
				callback(response);
			}
		}else{						
			response.errorCode = consts.Error.NONE_ERROR;
			response.message= consts.Message.OK_MESSAGE;
		
			response.data = JSON.stringify(shop);

			if(callback){
				callback(response);

			}

			return;
			
		}
		
	});

	return;
	
}




function processStartService(request,response,callback){
	
	console.log('processStartService');
	var data =request.data;
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}

	var param = null;
	try {
	    param = JSON.parse(data);
	} catch (e) {
		
	}
	
	if(param===null){
			response.errorCode=consts.Error.ERROR_PARAM;
			response.message=consts.Message.ERROR_PARAM;
			if(callback){
				callback(response);
			}
			return false;
	}

	if((param.IDs ===null || typeof(param.IDs)==='undefined') ) {
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':IDs';
		if(callback){
				callback(response);
		}
		return false;	
	}
	
	
	users.startService(request.token, param, function(shop){
		if(shop===null){

			response.errorCode=-1;
			response.message='更改数据错误4，请重新登录试试';
			if(callback){
				callback(response);
			}
		}else{						
			response.errorCode = consts.Error.NONE_ERROR;
			response.message= consts.Message.OK_MESSAGE;
		
//			response.data = JSON.stringify(shop);

			if(callback){
				callback(response);

			}

			return;
			
		}
		
	});

	return;
	
}


function processUserRefreshList1(request,response,callback){

	var data =request.data;
	
	if(!request.token){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	
	var baseline =0;
	var count=12;
   var direct=0;
    
	var param = null;

	if(!data){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;

	}else {
		console.log(data);
	}
	
	try {
	    param = JSON.parse(data);

	} catch (e) {
		
	
	}

	if(param!==null){
		if(param.baseline){
			baseline=param.baseline;
		}
		
		if(param.count){
			count=param.count;
		}
		
		
		if(param.direct){
			direct=param.direct;
		
		}
		
		
	}
	
	//token,pageNO,len, totalCount, , 
	var totalCount =0;
	
	users.getUserRefreshList1(request.token,baseline,count,direct,totalCount, function(err,wrap){
		if(err){
			
			response.errorCode=err.number;
			response.message=err.message;
			if(callback){
				callback(response);
			}
			return;
		}
		
		response.errorCode = consts.Error.NONE_ERROR;
		response.message= consts.Message.OK_MESSAGE;
		response.data = JSON.stringify(wrap);
		if(callback){
			callback(response);
		}
		 
	});

	return;

}






function processMyShopOperating(request,response,callback){
	
	console.log('processMyShopOperating');
	var data =request.data;
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}

	var param = null;
	try {
	    param = JSON.parse(data);
	} catch (e) {
		
	}

	if(param===null){
			response.errorCode=consts.Error.ERROR_PARAM;
			response.message=consts.Message.ERROR_PARAM;
			if(callback){
				callback(response);
			}
			return false;
	}
	if(param.ID ===null || typeof(param.ID)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':ID';
		if(callback){
				callback(response);
		}
		return false;	
	}
	
	if(param.isClose ===null || typeof(param.isClose)==='undefined'){
		param.isClose = 1;
	}
	
	users.updateMyShop(request.token, param, function(shop){
		if(shop===null){

			response.errorCode=-1;
			response.message='更改数据错误5，请重新登录试试';
			if(callback){
				callback(response);
			}
		}else{						
			response.errorCode = consts.Error.NONE_ERROR;
			response.message= consts.Message.OK_MESSAGE;
		
//			response.data = JSON.stringify(shop);

			response.token=request.token;
			if(callback){
				callback(response);

			}

			return;
//			response.errorCode = consts.Error.NONE_ERROR;
//			response.message= consts.Message.OK_MESSAGE;
//			ob=new Object();
//			ob.count=list.length;
//			ob.items=list;
//			response.data = JSON.stringify(ob);
//			response.token=tk;
//			if(callback){
//				callback(response);
//			}
			
		}
		
	});

	return;
	
}




function processCreditList(request,response,callback){
	
	console.log('processCreditList');
	var data =request.data;
	var token = null;
	var muuid = null;
	if(request.token===null || typeof(request.token)==='undefined'){
		
	}else {
		token = request.token;
	}
	
    if(request.muuid===null || typeof(request.muuid)==='undefined'){
		
	}else {
		muuid = request.muuid;
	}
    if(token === null && muuid ===null){
    	response.errorCode=consts.Error.NEED_LOGIN;
    	response.message=consts.Message.NEED_LOGIN;
    	if(callback){
    		callback(response);
    	}
    	return;
    }
	

	users.creditTemplateList(token,muuid, function(wrap){
		if(wrap===null || typeof(wrap)==='undefined'){
			response.errorCode=consts.Error.NEED_LOGIN;
			response.message=consts.Message.NEED_LOGIN;
			if(callback){
				callback(response);
			}
			return;
		}
		
		
		response.errorCode = consts.Error.NONE_ERROR;
		response.message= consts.Message.OK_MESSAGE;
		response.data = JSON.stringify(wrap);
		response.token=request.token;
		if(callback){
			callback(response);
		}
		
	});

	return;
	
}


function processSetPrinterCID(request,response,callback){
	
	console.log('processSet Print ClientID');
	var data =request.data;
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}

	var param = null;
	try {
	    param = JSON.parse(data);
	} catch (e) {
		
	}

	if(param===null){
			response.errorCode=consts.Error.ERROR_PARAM;
			response.message=consts.Message.ERROR_PARAM;
			if(callback){
				callback(response);
			}
			return false;
	}
	if(param.printCID ===null || typeof(param.printCID)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':printCID';
		if(callback){
				callback(response);
		}
		return false;	
	}
	
	
	users.setPrinterCID(request.token, param.printCID, function(error){
		if(error!==null){

			response.errorCode=-1;
			response.message='更改数据错误7，请重新登录试试';
			if(callback){
				callback(response);
			}
		}else{						
			response.errorCode = consts.Error.NONE_ERROR;
			response.message= consts.Message.OK_MESSAGE;
		
			response.token=request.token;
			if(callback){
				callback(response);

			}

			return;
			
		}
		
	});

	return;
	
}



async function processMemberPaymentReport(request,response,callback){
	
	console.log('processMemberPaymentReport::'+ request.data);
	
	var data =request.data;
	
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	
	var param = null;
	
	try {
		param=JSON.parse(data);
	} catch (e) {
	}
	var type = 1;
//	value = new Date().get
	if(param !==null && typeof(param.type) !== 'undefined' &&  param.type !== null )  {
		type = param.type;
	} 
	
	if(type !==1 && type !==7 && type!== 30 &&type !==12  && type !== 4) {
		type =1;
	}
	var datetime = new Date();
	var datetimes =[];
	
	var storeID = -1;
	if(param !==null && typeof(param.datetime) !== 'undefined' &&  param.datetime !== null &&  param.datetime !== 0 )  {
		datetime.setTime(param.datetime);
	//	datetimes.push(datetime);
	}
	
	if(param !==null && typeof(param.datetimes) !== 'undefined' &&  param.datetimes !== null &&  !isNaN(param.param.datetimes.length) )  {
		
		var ss =0;
		for(var i=0;i<param.datetimes.length;i++){
			datetime = new Date();
			datetime.setTime(param.datetime[i]);
			ss=0;
			for(var j=0;j<datatimes.length;j++){
				if(datetime===datetimes[j]){
					ss=1;
				}
			}
			if(ss===0){
				datetimes.push(datetime);
			}
		}
	}
	
	
	if(param !==null && typeof(param.storeID) !== 'undefined' &&  param.storeID !== null &&  param.storeID !== 0 )  {
		storeID =  param.storeID;
	}
	var wrap = null;
	var err = null;
	try{
		if(datetimes.length<=0){
			wrap = await member.paymentStatisticsReport(request.token,storeID, type,datetime,null);
		}else{
			wrap = await member.paymentStatisticsReports(request.token,storeID, type,datetimes,null);
		}
		
	}catch(e){
		err = e;
	}

	if(err){
		response.errorCode = err.number;
		response.message= consts.message;
		response.data = JSON.stringify(wrap);
		if(callback){
			callback(response);
		}
		
	}else {
		response.errorCode = consts.Error.NONE_ERROR;
		response.message= consts.Message.OK_MESSAGE;
		response.data = JSON.stringify(wrap);
		if(callback){
			callback(response);
		}
	
	}
			
	
	
	return true;
	
	
}






function processMyShopUpdate(request,response,callback){
	
	console.log('processMyShopUpdate');
	var data =request.data;
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}

	var param = null;
	try {
	    param = JSON.parse(data);
	} catch (e) {
		
	}

	if(param===null){
			response.errorCode=consts.Error.ERROR_PARAM;
			response.message=consts.Message.ERROR_PARAM;
			if(callback){
				callback(response);
			}
			return false;
	}
	if(param.ID ===null || typeof(param.ID)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':ID';
		if(callback){
				callback(response);
		}
		return false;	
	}
	
	
	users.updateMyShop(request.token, param, function(shop){
		if(shop===null){

			response.errorCode=-1;
			response.message='更改数据错误7，请重新登录试试';
			if(callback){
				callback(response);
			}
		}else{						
			response.errorCode = consts.Error.NONE_ERROR;
			response.message= consts.Message.OK_MESSAGE;
		
//			response.data = JSON.stringify(shop);

			response.token=request.token;
			if(callback){
				callback(response);

			}

			return;
//			response.errorCode = consts.Error.NONE_ERROR;
//			response.message= consts.Message.OK_MESSAGE;
//			ob=new Object();
//			ob.count=list.length;
//			ob.items=list;
//			response.data = JSON.stringify(ob);
//			response.token=tk;
//			if(callback){
//				callback(response);
//			}
			
		}
		
	});

	return;
	
}



function processUserBuyerList(request,response,callback){
	console.log('processUserBuyerList');
	
	var data =request.data;
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	
	users.buyerList(request.token, function(wrap){
		if(wrap===null){

			response.errorCode=-1;
			response.message='更改数据错误，请重新登录试试';
			if(callback){
				callback(response);
			}
		}else{

			response.errorCode = consts.Error.NONE_ERROR;
			response.message= consts.Message.OK_MESSAGE;
			response.data = JSON.stringify(wrap);
			response.token=request.token;
			if(callback){
				callback(response);
			}

			return;
			
		}
		
	});

	return;
	
}




function processPrinterList(request,response,callback){

	var data =request.data;
	
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	
	
	users.printerList(request.token,function(wrap){
		if(wrap===null || typeof(wrap)==='undefined'){
			response.errorCode=consts.Error.NEED_LOGIN;
			response.message=consts.Message.NEED_LOGIN;
			if(callback){
				callback(response);
			}
			return;
		}
		
		
		response.errorCode = consts.Error.NONE_ERROR;
		response.message= consts.Message.OK_MESSAGE;
		response.data = JSON.stringify(wrap);
		response.token=request.token;
		if(callback){
			callback(response);
		}
		
		

	
	});

}



function processContactDelete(request,response,callback){
	console.log('processContatDelete');

	var data =request.data;
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	if(data===null || typeof(data)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;

	}else {
		console.log(data);
	}
    var  param = null;
    
    try {
    	param = JSON.parse(data);
	} catch (e) {
	}

	if(param===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;
	}

	if(param.ID ===null || typeof(param.ID)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':ID';
		if(callback){
			callback(response);
		}
		return false;
	}
	
	cache.getByToken(request.token,function(tk,entity){
		if(tk===null || typeof(tk)==='undefined'){
			response.errorCode=consts.Error.NEED_LOGIN;
			response.message=consts.Message.NEED_LOGIN;
			if(callback){
				callback(response);
			}
			return;
		}
		contacts.remove(entity.userID,param.ID,function(err){
			if(err!==null){
				response.errorCode=err.number;
				response.message=err.message;
				if(callback){
					callback(response);
				}
			}else{
				response.errorCode = consts.Error.NONE_ERROR;
				response.message= consts.Message.OK_MESSAGE;
				response.token=tk;
				if(callback){
					callback(response);
				}			
			}			
		});
	});
}

function processUserBuyerDetail(request,response,callback){

	console.log('processUserBuyerList');
	
	var data =request.data;
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	
	if(data===null || typeof(data)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;

	}else {
		console.log(data);
	}
    var param = null;
    
    try {
    	param = JSON.parse(data);
	} catch (e) {
	}

	if(param===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;
	}

	if(param.ID ===null || typeof(param.ID)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':ID';
		if(callback){
			callback(response);
		}
		return false;
	}
	
	users.buyerDetailByID(request.token, param.ID, function(wrap){
		if(wrap===null){

			response.errorCode=-1;
			response.message='非在线支付方式无法在此获取会员详情！';
			if(callback){
				callback(response);
			}
		}else{

			response.errorCode = consts.Error.NONE_ERROR;
			response.message= consts.Message.OK_MESSAGE;
			response.data = JSON.stringify(wrap);
			response.token=request.token;
			if(callback){
				callback(response);
			}
			return;
			
		}
		
	});

	return;


}




function processDiscountList(request,response,callback){

	var data =request.data;
	
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	
	//token,pageNO,len, totalCount, , 
	
	users.getDiscountList(request.token,function(err,wrap){
		if(err){
			
			response.errorCode=err.number;
			response.message=err.message;
			if(callback){
				callback(response);
			}
			return;
		}
		
		response.errorCode = consts.Error.NONE_ERROR;
		response.message= consts.Message.OK_MESSAGE;
		response.data = JSON.stringify(wrap);
		if(callback){
			callback(response);
		}
		 
	});

	return;

}




function processDeskList(request,response,callback){

	var data =request.data;
	
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	
	
	var pageNO =0;
	var pageSize=12;
	var totalCount=0;
	var deskStatus = -1;
	var search =  null;
	
	
	var param = null;

	if(data===null || typeof(data)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;

	}else {
		console.log(data);
	}
	
	try {
	    param = JSON.parse(data);

	} catch (e) {
		
	
	}

	if(param!==null){
		if(param.pageNO===null || typeof(param.pageNO)==='undefined'){
			
		}else{
			pageNO=param.pageNO;
			if(pageNO < 0)pageNO=0;
		}
		
		if(param.pageSize===null || typeof(param.pageSize)==='undefined'){
			
		}else{
			pageSize=param.pageSize;
		}
		
		
		if(param.totalCount===null || typeof(param.totalCount)==='undefined'){
				
		
		}else{
			totalCount=param.totalCount;
		
		}
		
		if(param.search===null || typeof(param.search)==='undefined'){
				
		
		}else{
			search=param.search;
		
		}
	
	    if(param.deskStatus===null || typeof(param.deskStatus)==='undefined'){
			
		}else{
			deskStatus=param.deskStatus;
		}
	    
       
		
		
	}
	
	//token,pageNO,len, totalCount, , 
	
	users.getDeskList(request.token,pageNO, pageSize,totalCount,deskStatus,search,function(err,wrap){
		if(err){
			
			response.errorCode=err.number;
			response.message=err.message;
			if(callback){
				callback(response);
			}
			return;
		}
		
		response.errorCode = consts.Error.NONE_ERROR;
		response.message= consts.Message.OK_MESSAGE;
		response.data = JSON.stringify(wrap);
		if(callback){
			callback(response);
		}
		 
	});

	return;

}



function processDeskInfo(request,response,callback){

	var data =request.data;
	
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	
	
	var deskNO =null;
	
	var param = null;

	if(data===null || typeof(data)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;

	}else {
		console.log(data);
	}
	
	try {
	    param = JSON.parse(data);

	} catch (e) {
		
	
	}

	if(param!==null){
		if(param.deskNO===null || typeof(param.deskNO)==='undefined'){
			
		}else{
			deskNO=param.deskNO;
		}
		
	}
	
	//token,pageNO,len, totalCount, , 
	
	users.getDeskInfo(request.token,deskNO, function(err,wrap){
		if(err){
			
			response.errorCode=err.number;
			response.message=err.message;
			if(callback){
				callback(response);
			}
			return;
		}
		
		response.errorCode = consts.Error.NONE_ERROR;
		response.message= consts.Message.OK_MESSAGE;
		response.data = JSON.stringify(wrap);
		if(callback){
			callback(response);
		}
		 
	});

	return;

}



function processContactAdd(request,response,callback){

	var data =request.data;
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}

	if(data===null || typeof(data)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;

	}else {
	}
    var  param = null;
    

    try {
    	param = JSON.parse(data);
	} catch (e) {
	}
	


	if(param===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;
	}

	if(param.IDType ===null || typeof(param.IDType)==='undefined'){
		
		param.IDType=0;
	}

	
	if(param.cardType ===null || typeof(param.cardType)==='undefined'){
		param.cardType=0;
	}
	
	if(param.cardNO ===null || typeof(param.cardNO)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':cardNO';
		if(callback){
			callback(response);
		}
		return false;
	}

	if(param.name ===null || typeof(param.name)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':name';
		if(callback){
			callback(response);
		}

		return false;
	}
	


	if(param.gender ===null || typeof(param.gender)==='undefined'){
	
		 param.gender=0;
	}


	
	if(param.phone ===null || typeof(param.phone)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':phone';
		if(callback){
			callback(response);
		}
		return false;
	}
	
	console.log('6666666');

	
	cache.getByToken(request.token,function(tk,entity){
		if(tk===null | typeof(tk)==='undefined'){
			response.errorCode=consts.Error.NEED_LOGIN;
			response.message=consts.Message.NEED_LOGIN;
			if(callback){
				callback(response);
			}
			return;
		}
		param.userID=entity.userID;
		contacts.insert(param,function(err, obj){
			if(err!==null){
				response.errorCode=err.number;
				response.message=err.message;
				if(callback){
					callback(response);
				}
			}else{
				
				response.errorCode = consts.Error.NONE_ERROR;
				response.message= consts.Message.OK_MESSAGE;
				response.data=JSON.stringify(obj);
				
//				ob=new Object();
//				ob.count=list.length;
//				ob.items=list;
//				response.data = JSON.stringify(ob);
//				response.token=tk;
				if(callback){
					callback(response);
	
				}
				
			}
			
		});

	
		
	});

}







function processUserDetail(request,response,callback){
	console.log('processUserDetail');
	
	var data =request.data;
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	
	
	cache.getByToken(request.token,function(tk,entity){
		if(tk===null || typeof(tk)==='undefined'){
			response.errorCode=consts.Error.NEED_LOGIN;
			response.message=consts.Message.NEED_LOGIN;
			if(callback){
				callback(response);
			}
			return;
		}
		users.getEntityByID(entity.userID,function(entity){
			if(entity===null){
				response.errorCode=consts.Error.NEED_LOGIN;
				response.message=consts.Message.NEED_LOGIN;
				if(callback){
					callback(response);
				}
			}else{
				
				if(entity){
					im.getIMToken(entity,function(err){
						
						cache.refresh(entity,function(tk,user){
							if(tk===null || typeof(tk)==='undefined'){
								response.errorCode=consts.Error.NEED_LOGIN;
								response.message=consts.Message.NEED_LOGIN;
								if(callback){
									callback(response);
								}
							}else{
								response.errorCode = consts.Error.NONE_ERROR;
								response.message= consts.Message.OK_MESSAGE;
								response.data = JSON.stringify(user);
								response.token=tk;
								if(callback){
									callback(response);
								}
							}
						});
						
					});
					
				}else{
						
					response.errorCode = consts.Error.NONE_ERROR;
					response.message= consts.Message.OK_MESSAGE;
					response.data = JSON.stringify(user);
					response.token=tk;
					if(callback){
						callback(response);
					}
			
				
				}
			
			}
			
		});

	
		
	});

}



function processUserDetailUpdate(request,response,callback){
	console.log('processUserDetailUpdate');

	var data =request.data;
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	
	if(data===null || typeof(data)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;

	}else {
		console.log(data);
	}
    var param = null;
    
    try {
    	param = JSON.parse(data);
	} catch (e) {
	}

	if(param===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;
	}
	

	var changevals = [];
    var	i=0;
	if(param.iconURL!==null){
		changevals.icon_url=param.iconURL;
		i++;
	}
	
	if(param.nickName!==null){
		changevals.nick_name=param.nickName;
		i++;

	}
	
	console.log(param.gender);
	
	if(typeof(param.gender)!=='undefined' && param.gender !== null){
		changevals.gender=param.gender;
		i++;

	}
	if(param.mobile!==null){
		changevals.phone_number=param.mobile;
		i++;

	}
	if(param.postalAddress!==null){
		changevals.postal_address=param.postalAddress;
		i++;

	}
	
	if(param.birthDay!==null){
		changevals.birth_date=param.birthDay;
		i++;

	}
	
	if(param.email!==null){
		changevals.address=param.email;
		i++;

	}
	

	if(i<=0){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;
	}
	

	cache.getByToken(request.token,function(tk,entity){
		if(tk===null || typeof(tk)==='undefined'){
			response.errorCode=consts.Error.NEED_LOGIN;
			response.message=consts.Message.NEED_LOGIN;
			if(callback){
				callback(response);
			}
			return;
		}
		
		entity.qiniuToken=qiniu.uptoken(null);
		users.update(entity.userID,changevals,function(err,entity){
			if(err){
				response.errorCode=err.number;
				response.message=err.message;
				if(callback){
					callback(response);
				}
			}else{
				tournote_user.updateExist(entity,null);
				              
				entity.qiniuToken=qiniu.uptoken(null);
				
				im.getIMToken(entity,function(err){
						cache.refresh(entity,function(tk,user){
							if(tk===null || typeof(tk)==='undefined'){
								response.errorCode=consts.Error.NEED_LOGIN;
								response.message=consts.Message.NEED_LOGIN;
								if(callback){
									callback(response);
								}
							}else{
								response.errorCode = consts.Error.NONE_ERROR;
								response.message= consts.Message.OK_MESSAGE;
								response.data = JSON.stringify(user);
								response.token=tk;
								if(callback){
									callback(response);
								}
							}
						});
						
				});
						
			}
		});
	});

}


module.exports = {
		processUserDetail:processUserDetail,	
		processUserDetailUpdate:processUserDetailUpdate,
		processUserLook:processUserLook,
		processContactAdd:processContactAdd,
		processMyShopLook:processMyShopLook,
		processMyShopUpdate:processMyShopUpdate,
		processMyShopOperating:processMyShopOperating,
		processUserBuyerList:processUserBuyerList,
		processContactDelete:processContactDelete,
		processUserBuyerDetail:processUserBuyerDetail,
		processUsersLook:processUsersLook,
		processBuildScanURL:processBuildScanURL,
		processPrinterList:processPrinterList,
		processCompleteService:processCompleteService,
		processCompleteService1:processCompleteService1,
		processDeskList:processDeskList,
		processDiscountList:processDiscountList,
		processStartService:processStartService,
		processRefundUpdatePwd:processRefundUpdatePwd,
      processRefundVerifyPwd:processRefundVerifyPwd,
      processDeskInfo:processDeskInfo,
      processSetPrinterCID:processSetPrinterCID,
      processCreditList:processCreditList,
      processMemberPaymentReport:processMemberPaymentReport,
      processUserRefreshList1:processUserRefreshList1,


};

